package com.firstcare.android_dylibrary_demo.Mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.firstcare.android_dylibrary.bean.AccountProxy;
import com.firstcare.android_dylibrary_demo.BuildConfig;
import com.firstcare.android_dylibrary_demo.Main.MainActivity;
import com.firstcare.android_dylibrary_demo.R;
import com.rdxer.xxlibrary.HTTPUtils.error.ProcessingError;
import com.rdxer.xxlibrary.HTTPUtils.listener.ErrorListener;
import com.rdxer.xxlibrary.HTTPUtils.listener.FailedListener;
import com.rdxer.xxlibrary.HTTPUtils.listener.OKListener;
import com.rdxer.xxlibrary.OtherUtils.KeyBoardUtils;
import com.rdxer.xxlibrary.OtherUtils.Matcher.MatcherError;
import com.rdxer.xxlibrary.OtherUtils.Matcher.MatcherUtils;
import com.rdxer.xxlibrary.base.BaseActivity;
import com.rdxer.xxlibrary.utils.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by LXF on 16/6/12.
 */

public class LoginActivity extends BaseActivity {
    EditText edtAccount;
    EditText edtPassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        edtAccount = (EditText) findViewById(R.id.userNameEditText);
        edtPassword = (EditText) findViewById(R.id.passwordEditText);
        if (BuildConfig.DEBUG) {
            edtAccount.setText("18611153241");
            edtPassword.setText("123456");
        }
    }


    public void loginButtonClick(View view) {
        MatcherUtils m = new MatcherUtils() {
//            如果输入的有 button
//            @Override
//            protected String getString(Object value) {
//                if (value instanceof Button) {
//                    return ((Button) value).getText().toString().trim();
//                }
//                return super.getString(value);
//            }
        };
        try {
            m.matcherPhoneNumber(edtAccount);
            m.matcherPassword(edtPassword);
        } catch (MatcherError matcherError) {
            Toast.makeText(this, "请输入合适的账号名密码", Toast.LENGTH_SHORT).show();
            if (matcherError.getErrorCode() != 0) {
                if (matcherError.getValue() instanceof EditText) {
                    KeyBoardUtils.openKeybord((EditText) matcherError.getValue(), LoginActivity.this);
                    ((EditText) matcherError.getValue()).requestFocus();
                }
            }
            return;
        }

        AccountProxy accountProxy = new AccountProxy(edtAccount.getText().toString().trim(), edtPassword.getText().toString().trim());
        accountProxy.login(new OKListener<AccountProxy>() {
            @Override
            public void onOK(AccountProxy res, JSONObject response) {
                Toast.makeText(LoginActivity.this, "登陆成功...\n" + res, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                // 携带参数跳转
                // 参数 res (类型:AccountProxy)
//                LoginActivity.this.startActivity(intent,res);
                LoginActivity.this.startActivity(intent);
//                startActivityForResult(intent,0,res);
            }
        }, new FailedListener() {
            @Override
            public void onFailed(ProcessingError error) {

                Toast.makeText(LoginActivity.this, "登陆失败...." + error.getMessage(), Toast.LENGTH_SHORT).show();
                Log.e("onFailed: ", error);
            }
        }, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(LoginActivity.this, "网络异常..." + error, Toast.LENGTH_SHORT).show();
                Log.e("onErrorResponse: " + error);
            }
        });
    }

    /// 重写此方法 也可以  取参数 使用 getReverseParames(data)
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode == 0){
//            Log.e(getReverseParames(data));
//        }
//    }

    /// 此方法直接 解析 参数 reverseParames
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data, Object reverseParames) {
        if (requestCode == 0) {
            Log.e(reverseParames);
        }
    }

//    public static final String s = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
    
    @OnClick(R.id.button6)
    public void onClick() {
        MatcherUtils matcherUtils = new MatcherUtils();
        try {
            matcherUtils.matcherEmilOrPhoneNumber("a240302832@qq.com","12345677777");
        } catch (MatcherError matcherError) {
            Toast.makeText(LoginActivity.this, ""+matcherError.getValue(), Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(LoginActivity.this, "OK", Toast.LENGTH_SHORT).show();
    }
}
